// route_observer.dart
import 'package:asset_management/navigation/router_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class CustomRouteObserver extends NavigatorObserver {
  final Ref _ref;

  CustomRouteObserver(this._ref);

  @override
  void didPop(Route route, Route? previousRoute) {
    _updateState(previousRoute);
  }

  @override
  void didPush(Route route, Route? previousRoute) {
    _updateState(route);
  }

  void _updateState(Route? currentRoute) {
    if (currentRoute == null) return;
    final canPop = currentRoute.navigator?.canPop() ?? false;
    _ref.read(routeNotifierProvider.notifier).updateNavigationState(canPop);
  }
}
